﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web.UI;
using EPiServer.Core;
using EPiServer.Web.WebControls;

namespace TemplateFoundation.WebControls
{
    /// <summary>
    /// Adds an AlternatingItemTemplate to the standard EPiServer PageList control
    /// </summary>
    public class PageList : EPiServer.Web.WebControls.PageList
    {
        private ITemplate _alternatingItemTemplate;

        /// <summary>
        /// Instantiates the item templates and renders their contents
        /// </summary>
        protected override void CreateChildControls()
        {
            if (ItemTemplate != null)
            {
                PageDataCollection pages = GetPages();
                if (pages.Count != 0)
                {
                    PageData page = null;
                    
                    if (!PageReference.IsNullOrEmpty(PageLink))
                    {
                        page = GetPage(PageLink);
                    }
                    
                    if (HeaderTemplate != null)
                    {
                        Control container = new PageTemplateContainer(page);
                        HeaderTemplate.InstantiateIn(container);
                        Controls.Add(container);
                    }
                    
                    PreparePagingControls(pages);
                    
                    for (int i = 0; i < pages.Count; i++)
                    {
                        Control control2 = new PageTemplateContainer(pages[i]);

                        if (i%2==0 || AlternatingItemTemplate==null)
                        {
                            ItemTemplate.InstantiateIn(control2);
                        }
                        else
                        {
                            AlternatingItemTemplate.InstantiateIn(control2);
                        }

                        Controls.Add(control2);
                    }

                    if (FooterTemplate != null)
                    {
                        Control control3 = new PageTemplateContainer(page);
                        FooterTemplate.InstantiateIn(control3);
                        Controls.Add(control3);
                    }
                    
                    CreatePagingControls(pages);
                }
            }
        }

        /// <summary>
        /// Gets or sets the alternating item template.
        /// </summary>
        /// <value>The alternating item template.</value>
        [Browsable(false), TemplateContainer(typeof(PageTemplateContainer)), PersistenceMode(PersistenceMode.InnerProperty)]
        public ITemplate AlternatingItemTemplate
        {
            get
            {
                return _alternatingItemTemplate;
            }
            set
            {
                _alternatingItemTemplate = value;
            }
        }

    }
}
